package com.example.im.db.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.im.bean.UserRoom;

import java.util.List;

@Dao
public interface UserRoomDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    Long insert(UserRoom userRoom);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(List<UserRoom> userRooms);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void update(UserRoom userRoom);

    @Delete
    void delete(UserRoom userRoom);

    @Query("SELECT * FROM UserRoom")
    List<UserRoom> loadAll();

    @Query("SELECT * FROM UserRoom WHERE account =:account")
    UserRoom selectUserRoomByAccount(String account);

    @Query("SELECT * FROM UserRoom WHERE id =:id")
    UserRoom selectUserRoomByID(int id);

    @Query("SELECT * FROM UserRoom WHERE type =:type AND (sub = 4 OR sub = 3 OR sub = 2) AND nick_name IS NOT NULL")
    List<UserRoom> selectUserRoomByType(int type);

    @Query("SELECT * FROM UserRoom WHERE type =:type AND nick_name IS NOT NULL")
    List<UserRoom> selectUserRoomByTypeAll(int type);

    @Query("SELECT * FROM UserRoom WHERE type =:type AND sub =:sub AND nick_name IS NULL")
    List<UserRoom> selectUserRoomByTypeAndSub(int type, int sub);


    @Query("SELECT * FROM UserRoom WHERE account LIKE '%' || :search || '%' OR nick_name LIKE '%' || :search || '%'")
    List<UserRoom> selectUserRoomBySearch(String search);

    @Query("SELECT COUNT(*) FROM UserRoom WHERE type =1 AND sub =2 AND nick_name IS NULL")
    int countUnread();

    @Query("UPDATE UserRoom SET sub= :sub WHERE id = :uid")
    void upDateUserRoomSub(int sub, int uid);
}
